package com.dgshanger.sanhxiaofeisc.mvp.presenter;

import com.dgshanger.sanhxiaofeisc.entity.HomeEntity;
import com.dgshanger.sanhxiaofeisc.entity.NearbyItemEntity;
import com.dgshanger.sanhxiaofeisc.mvp.contract.NearbyContract;
import com.dgshanger.sanhxiaofeisc.mvp.model.NearbyModel;
import com.xx.baseuilibrary.mvp.BaseMvpPresenter;
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * NearbyPresenter
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/4/19 10:39.
 */

public class NearbyPresenter extends BaseMvpPresenter<NearbyContract.Model, NearbyContract.View> implements NearbyContract.Presenter {
    @NotNull
    @Override
    protected NearbyContract.Model createModel() {
        return new NearbyModel();
    }

    @Override
    public void loadCategory() {
        getModel().loadCategory(new XxBaseHttpObserver<List<HomeEntity.CategoryListBean>>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable List<HomeEntity.CategoryListBean> entity) {
                if (getView() == null)
                    return;
                getView().upDateCategoryList(entity);
            }

            @Override
            public void onError(@Nullable String error) {

            }

            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }
        });
    }

    @Override
    public void loadMerchant(boolean refresh, String location_name, String sort_distance, String user_longitude, String user_latitude, String page, String sort_new, String sort_comment) {
        getModel().loadMerchant(location_name, sort_distance, user_longitude, user_latitude, page, sort_new,
                sort_comment, new XxBaseHttpObserver<List<NearbyItemEntity>>() {
                    @Override
                    public void onCompleted(@Nullable String msg, @Nullable List<NearbyItemEntity> entity) {
                        if (getView() == null)
                            return;
                        if (refresh){
                            getView().setData(entity);
                        }else {
                            getView().upDateAddMerchantList(entity);
                        }
                    }

                    @Override
                    public void onError(@Nullable String error) {
                        if (getView() == null)
                            return;
                        getView().showError(new Throwable(error),refresh);
                        getView().onDataFailure(error);
                    }

                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onFinish() {
                        if (getView() == null)
                            return;
                        getView().dismissLoadingDialog();
                    }
                });
    }


}
